package com.panda.pandawash.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.panda.pandawash.R;
import com.panda.pandawash.application.CDApplication;
import com.panda.pandawash.bean.HttpResultBean;
import com.panda.pandawash.bean.StrongBoxBean;
import com.panda.pandawash.util.HttpUtil;
import com.panda.pandawash.util.WsUtils;
import com.panda.pandawash.view.StatusBarCompat;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by gaowf on 2017/2/10.
 */

public class ModifySafePsdActivity extends Activity {

    private String TAG = "ModifySafePsdActivity";

    @ViewInject(R.id.safe_modify_psd_return)
    private ImageView imageView;

    @ViewInject(R.id.btn_confirm_modify_psd_safe_box)
    private Button confirmButton;

    private EditText mOldPswd;
    private EditText mNewPswd;
    private EditText mConfirmPswd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_safe_box_modify_psd);
        x.view().inject(this);
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT) {
            StatusBarCompat.compat(this, getResources().getColor(R.color.colorPrimary));
        }
        initResource();

        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                comparePassword();
            }

        });

    }

    protected void comparePassword() {
        String password0 = mOldPswd.getText().toString().trim();
        String password1 = mNewPswd.getText().toString().trim();
        String password2 = mConfirmPswd.getText().toString().trim();
        Log.d("hys123","getInputPsd "+ password1 +" getConfirmPsd " + password2);

        if (!WsUtils.isMatch(WsUtils.REGEX_STRONGPASSWORD, password0) ||
                !WsUtils.isMatch(WsUtils.REGEX_STRONGPASSWORD, password1) ||
                !WsUtils.isMatch(WsUtils.REGEX_STRONGPASSWORD, password2)) {
            showMessage(getResources().getString(R.string.safe_box_password_legal));
            return;
        }

        if (password1.equals(password2) && password2 != null && password1 != null) {
            changeStrongBoxPsw();

        } else {
            showMessage(getResources().getString(R.string.registe_password_not_same));
        }
    }

    private void showMessage(String string) {
        Toast toast = Toast.makeText(ModifySafePsdActivity.this, string, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    private void initResource() {
        mOldPswd = (EditText) findViewById(R.id.edit_origin_psd_safe_box);
        mNewPswd = (EditText) findViewById(R.id.edit_new_psd_safe_box);
        mConfirmPswd = (EditText) findViewById(R.id.edit_confirm_psd_safe_box);
    }

    private void changeStrongBoxPsw(){
        String account = CDApplication.getIntance().getUserInfo().getUseraccount();
        String oldPassword = mOldPswd.getText().toString().trim();
        String newPassword = mConfirmPswd.getText().toString().trim();
        StrongBoxBean strongBoxBean = new StrongBoxBean();
        strongBoxBean.setSecretpassword(oldPassword);
        strongBoxBean.setNewsecretpassword(newPassword);
        HttpUtil.changeStrongBoxPSW(new SafeBoxChangeCallback(),account,account,strongBoxBean);
    }

    private class SafeBoxChangeCallback extends AsyncHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, String content) {
            super.onSuccess(statusCode, content);
            Log.d(TAG, "SafeBoxSetupCallback statusCode = " + statusCode);
            Log.d(TAG, "SafeBoxSetupCallback onSuccess = " + content);
            if (statusCode == 200) {
                HttpResultBean resultBean = HttpUtil.parserHttpResultInfo(content, null);
                Log.d(TAG, "resultBean = " + resultBean);
                if (resultBean != null) {
                    String code = resultBean.getCode();
                    String message = resultBean.getMessage();
                    Log.d(TAG, "code = " + code);
                    if(code.equals("0")){
                        showMessage(getResources().getString(R.string.safe_box_password_change_success));
                        finish();
                    }else if(code.equals("99")){
                        showMessage(message);
                    }
                }
            } else {
                showMessage(getResources().getString(R.string.safe_box_password_change_fail));
            }
            finish();
        }

        @Override
        public void onFailure(Throwable error, String content) {
            super.onFailure(error, content);

            Log.d(TAG, "onFailure = " + error);
            if (error.toString().contains("refused")) {

            }
            finish();
        }
    }


    @Event(R.id.safe_modify_psd_return)
    private void excuteReturn(View v) {
        finish();
    }
}
